package com.gc.ykqx.ui.fragment;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.gc.ykqx.R;
import com.gc.ykqx.ui.custom.LazyFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * A simple {@link Fragment} subclass.
 */
public class HisFragment extends LazyFragment implements TabLayout.OnTabSelectedListener {

    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.img)
    ImageView img;
    Unbinder unbinder;
    @BindView(R.id.btn_next)
    ImageButton btnNext;
    @BindView(R.id.btn_previous)
    ImageButton btnPrevious;
    private int currentTab;
    private int currentIndex;
/*    private int[] imgArray = {R.mipmap.pic_year, R.mipmap.pic_month_one, R.mipmap.pic_month_two,
            R.mipmap.pic_tem_one, R.mipmap.pic_tem_two, R.mipmap.pic_tem_three,
            R.mipmap.pic_pre_one, R.mipmap.pic_pre_two, R.mipmap.pic_pre_three};*/

    public HisFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_his, container, false);
        unbinder = ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        tabLayout.addTab(tabLayout.newTab().setText("年平均"));
        tabLayout.addTab(tabLayout.newTab().setText("月分布"));
        tabLayout.addTab(tabLayout.newTab().setText("历年温度"));
        tabLayout.addTab(tabLayout.newTab().setText("历年降水"));
        tabLayout.addOnTabSelectedListener(HisFragment.this);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        currentTab = tab.getPosition();
        currentIndex = 0;
        switch (currentTab) {
            case 0:
                img.setImageResource(R.mipmap.pic_year);
                btnNext.setVisibility(View.INVISIBLE);
                btnPrevious.setVisibility(View.INVISIBLE);
                break;
            case 1:
                img.setImageResource(R.mipmap.pic_month_one);
                btnNext.setVisibility(View.VISIBLE);
                btnPrevious.setVisibility(View.INVISIBLE);
                break;
            case 2:
                btnNext.setVisibility(View.VISIBLE);
                btnPrevious.setVisibility(View.INVISIBLE);
                img.setImageResource(R.mipmap.pic_tem_one);
                break;
            case 3:
                btnNext.setVisibility(View.VISIBLE);
                btnPrevious.setVisibility(View.INVISIBLE);
                img.setImageResource(R.mipmap.pic_pre_one);
                break;
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    protected void lazyLoad() {

    }

    @OnClick({R.id.btn_next, R.id.btn_previous})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_next:
                switch (currentTab) {
                    case 1:
                        if (currentIndex == 0) {
                            img.setImageResource(R.mipmap.pic_month_two);
                            currentIndex = 1;
                            btnNext.setVisibility(View.INVISIBLE);
                            btnPrevious.setVisibility(View.VISIBLE);
                        }
                        break;
                    case 2:
                        if (currentIndex == 0) {
                            img.setImageResource(R.mipmap.pic_tem_two);
                            currentIndex = 1;
                            btnNext.setVisibility(View.VISIBLE);
                            btnPrevious.setVisibility(View.VISIBLE);
                        } else if (currentIndex == 1) {
                            img.setImageResource(R.mipmap.pic_tem_three);
                            currentIndex = 2;
                            btnNext.setVisibility(View.INVISIBLE);
                            btnPrevious.setVisibility(View.VISIBLE);
                        }
                        break;
                    case 3:
                        if (currentIndex == 0) {
                            img.setImageResource(R.mipmap.pic_pre_two);
                            currentIndex = 1;
                            btnNext.setVisibility(View.VISIBLE);
                            btnPrevious.setVisibility(View.VISIBLE);
                        } else if (currentIndex == 1) {
                            img.setImageResource(R.mipmap.pic_pre_three);
                            currentIndex = 2;
                            btnNext.setVisibility(View.INVISIBLE);
                            btnPrevious.setVisibility(View.VISIBLE);
                        }
                        break;
                }
                break;
            case R.id.btn_previous:
                switch (currentTab) {
                    case 1:
                        if (currentIndex == 1) {
                            img.setImageResource(R.mipmap.pic_month_one);
                            currentIndex = 0;
                            btnNext.setVisibility(View.VISIBLE);
                            btnPrevious.setVisibility(View.INVISIBLE);
                        }
                        break;
                    case 2:
                        if (currentIndex == 1) {
                            img.setImageResource(R.mipmap.pic_tem_one);
                            currentIndex = 0;
                            btnPrevious.setVisibility(View.INVISIBLE);
                            btnNext.setVisibility(View.VISIBLE);
                        } else if (currentIndex == 2) {
                            currentIndex = 1;
                            img.setImageResource(R.mipmap.pic_tem_two);
                            btnNext.setVisibility(View.VISIBLE);
                            btnPrevious.setVisibility(View.VISIBLE);
                        }
                        break;
                    case 3:
                        if (currentIndex == 1) {
                            img.setImageResource(R.mipmap.pic_pre_one);
                            currentIndex = 0;
                            btnPrevious.setVisibility(View.INVISIBLE);
                            btnNext.setVisibility(View.VISIBLE);
                        } else if (currentIndex == 2) {
                            currentIndex = 1;
                            img.setImageResource(R.mipmap.pic_pre_two);
                            btnNext.setVisibility(View.VISIBLE);
                            btnPrevious.setVisibility(View.VISIBLE);
                        }
                        break;
                }
                break;
        }
    }
}
